<template>
  <el-dialog title="任务分配" :visible.sync="show" @close="close" width="60%"  append-to-body>

    <el-table ref="table" style="width: 100%" :data="userList" @row-click="handleRowClick">
      <el-table-column label="姓名" align="left" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="登录名" align="left" key="userName" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button :loading="loading" @click.stop="handleRowClick(scope.row)" type="primary" size="small">分配</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div slot="footer" class="dialog-footer">
<!--      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>-->
      <el-button @click="close">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateTodo } from '@/api/gd/todo'
export default {
  name: 'rwUserForm',
  props: {
    show: {
      type: Boolean
    },
    userList: {
      type: Array
    },
    data: {
      type: Object
    }
  },
  data() {
    return {
      loading: false,
      form: {}
    };
  },
  methods: {
    handleRowClick(row) {
      this.form.id = this.data.id
      this.form.title = this.data.title
      this.form.qsrid = row.userId
      this.form.qsr = row.nickName
      this.form.qssj = this.formatDateTime(new Date())
      this.form.status = "2"
      this.loading = true
      updateTodo(this.form).then(res => {
        if (res.code === 200) {
          this.$message.success("任务已分配给 '"+ row.nickName +"'")
        }
      }).finally( res => {
        this.loading = false
        this.$emit("closeList")
      })
    },
    close() {
      this.$emit("closeList")
    },
    formatDateTime(date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
  }

}
</script>

<style scoped>

</style>
